package net.htjs.mobile.nyoa;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.htjs.mobile.nyoa.gw.ListGwWebView;
import net.htjs.mobile.nyoa.subview.DragGridView;
import net.htjs.mobile.nyoa.util.L;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class nineActivity extends Activity {
	private DragGridView gridview; 
    private List meumList;  
	private static String[] appName = { "待办公文","在办公文","所有公文","电子公告","电子邮件","通讯录","规章制度" };

	public void onCreate(Bundle b) {
		super.onCreate(b);
		requestWindowFeature(Window.FEATURE_NO_TITLE); 
		setContentView(R.layout.nine);
		Resources res = getResources();
		Drawable drawable = res.getDrawable(R.drawable.background_login);
		this.getWindow().setBackgroundDrawable(drawable);
		gridview= (DragGridView) findViewById(R.id.GridView); // 构造GridView组件
		gridview.setNumColumns(3);

		meumList = new ArrayList();
		for (int i = 1; i < 8; i++) {
			
			HashMap<String, Object> map = new HashMap<String, Object>();

			map.put("ItemImage", R.drawable.icon);
			{
				if (i <= appName.length) {
					map.put("ItemText", appName[i - 1]);
				} else
					map.put("ItemText", "未知app " + (i - appName.length));
			}

			if(i==1){
				map.put("ItemImage", R.drawable.dbgw);
			}
			else if(i==2){
				map.put("ItemImage", R.drawable.zbgw);
			}
			else if(i==3){
				map.put("ItemImage", R.drawable.sygw);
			}
			else if(i==4){
				map.put("ItemImage", R.drawable.dzgg);
			}else if(i==5){
				map.put("ItemImage", R.drawable.dzyj);
			}else if(i==6){
				map.put("ItemImage", R.drawable.txl);
			}else if(i==7){
				map.put("ItemImage", R.drawable.gzzd);
			}
			meumList.add(map);
		}

		ImageAdapter saMenuItem = new ImageAdapter(this, meumList, // 数据源
				R.layout.menuitem, // xml实现
				new String[] { "ItemImage", "ItemText", "ItemText1" }, // 对应map的Key
				new int[] { R.id.ItemImage, R.id.ItemText}); // 对应R的Id

		gridview.setAdapter(saMenuItem);

		gridview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

				if (arg2 == 0) {
					Intent intent = new Intent();

					Bundle bundle = new Bundle();

					bundle.putString(L.ZT, "DB");
					bundle.putString(L.LX, L.Nine.GW);

					intent.setClass(nineActivity.this, ListGwWebView.class);

					intent.putExtras(bundle);

					startActivity(intent);
					return;
				}else if (arg2 == 1) {
					Intent intent = new Intent();

					Bundle bundle = new Bundle();

					bundle.putString(L.ZT, "ZB");
					bundle.putString(L.LX, L.Nine.GW);

					intent.setClass(nineActivity.this, ListGwWebView.class);

					intent.putExtras(bundle);

					startActivity(intent);
					return;
				}else if (arg2 == 2) {
					Intent intent = new Intent();

					Bundle bundle = new Bundle();

					bundle.putString(L.ZT, "SY");
					bundle.putString(L.LX, L.Nine.GW);

					intent.setClass(nineActivity.this ,ListGwWebView.class);

					intent.putExtras(bundle);

					startActivity(intent);
					return;
				}else if (arg2 == 3) {
					Intent intent = new Intent();

					Bundle bundle = new Bundle();

					bundle.putString(L.ZT, "SY");
					bundle.putString(L.LX, L.Nine.DZGG);

					intent.setClass(nineActivity.this ,ListGwWebView.class);

					intent.putExtras(bundle);

					startActivity(intent);
					return;
				}else if (arg2 == 4) {
					Intent intent = new Intent();

					Bundle bundle = new Bundle();

					bundle.putString(L.LX, L.Nine.DZYX);

					intent.setClass(nineActivity.this ,ListGwWebView.class);

					intent.putExtras(bundle);

					startActivity(intent);
					return;
				}else if (arg2 == 5) {
					Intent intent = new Intent();

					Bundle bundle = new Bundle();

					bundle.putString(L.LX, L.Nine.TXL);

					intent.setClass(nineActivity.this ,ListGwWebView.class);

					intent.putExtras(bundle);

					startActivity(intent);
					return;
				}else if (arg2 == 6) {
					Intent intent = new Intent();

					Bundle bundle = new Bundle();

					bundle.putString(L.LX, L.Nine.GZZD);

					intent.setClass(nineActivity.this ,ListGwWebView.class);

					intent.putExtras(bundle);

					startActivity(intent);
					return;
				}
			}
		});
	}	

	public class ImageAdapter extends SimpleAdapter{  
        private Context mContext;  
        private LayoutInflater lif; 
        int icon[];
        public ImageAdapter(Context c,List l,int t,String[] a,int[] i){  
            super(c, l, t, a, i);
        	mContext = c; 
            lif = LayoutInflater.from(c); 
            icon=i;
        }  
          
        public int getCount() {  
            return meumList.size();  
        }  
  
        public Object getItem(int position) {  
            // TODO Auto-generated method stub  
            return meumList.get(position);  
        }  
  
        public long getItemId(int position) {  
            // TODO Auto-generated method stub  
            return position;  
        }  
  
        /** 
         * 遇到的问题： 
         * 1、删除错了 会有重复的图片 
         * 这个是由 remove引起的 
         *  删除时 我们先删除了小的position 
         * 比如说 startPosition = 2;endPosition = 3; 
         * 当我先删startPosition时 这时 删除前position为3的项 已经是position为2了  
         * 2、数组越界 异常 
         * 这个是由 add引起的 
         * 比如说 startPosition = 8;endPosition = 7; 
         * 一共gridview有9个元素 也就是说8 已经是最大的了 
         * 当删除完后 你先增加爱 startposition时  就会异常了 
         * 3、preview问题 
         * 当我拖拽互换几次后 机会出现 当前的图片 显示的是另一个图片的preview 
         *  
         * 得调用 destroyDrawingCache 
         * @param startPosition 
         * @param endPosition 
         */  
        public void exchange(int startPosition, int endPosition){  
            //比较一下 使startPosition永远小于endPosition的值 解决问题1 ，2  
            if(startPosition > endPosition){  
                int temp = endPosition;  
                endPosition = startPosition;  
                startPosition = temp;  
            }  
            Object endObject = getItem(endPosition);  
            Object startObject = getItem(startPosition);  
            //list.remove(endPosition);  
            //list.remove(startPosition);  
            System.out.println(startPosition + "========"+endPosition);  
            meumList.set(startPosition,endObject);  
            meumList.set(endPosition,startObject);  
            notifyDataSetChanged();  
        }  
          
        public View getView(int position, View convertView, ViewGroup parent) {  
            //ImageView iv;  
            if(convertView==null){  
                convertView = lif.inflate(R.layout.menuitem, null);  
//                iv = new ImageView(mContext);  
//                try{  
//                Field f = (Field)R.drawable.class.getDeclaredField(list.get(position));  
//                int i = f.getInt(R.drawable.class);  
//                iv.setImageResource(i);  
//                }catch(Exception e){  
//                    e.printStackTrace();  
//                }  
            }  
                  
            try {  
             
                  
                TextView  textView=(TextView)convertView.findViewById(R.id.ItemText);  
                textView.setText(((Map)meumList.get(position)).get("ItemText").toString());  
                ImageView  imgView=(ImageView)convertView.findViewById(R.id.ItemImage); 
                Object o=((Map)meumList.get(position)).get("ItemImage");
                if(null==o){
                	imgView.setBackgroundResource(R.drawable.icon);
                }else{
                	 imgView.setBackgroundResource(Integer.valueOf(o.toString()));
                }
                 
                  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
            return convertView;  
        }  
          
    }  
}
